package com.redhat.redwork.widget.select;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.redhat.redwork.widget.DataModelNotFoundException;

@Component("selectApplication")
public class SelectApplicationImpl implements SelectApplication, ApplicationContextAware{

	protected ApplicationContext applicationContext;
	
	/**
	 * spring applicationContext auto inject
	 */
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	@Override
	public SelectModel findModel(String modelName) {
		if(modelName == null){
			throw new IllegalArgumentException("model name can not be null");
		}
		
		final String dataModelName = modelName + "SelectModel";
		
		if(!applicationContext.containsBean(dataModelName)){
			throw new DataModelNotFoundException();
		}
		
		SelectModel model = applicationContext.getBean(dataModelName, SelectModel.class);
		return model;
	}
}
